
//获取应用实例
const app = getApp()
var commonData = require('../../data/data.js');
var commonController = require('../../data/commonControll.js');
Page({
  data: {
    nickName: "",
    userImg: "",
    catPrice: "",
    isShow:true,
    height:600,
    isShowLogin: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    version:-1,
    btnname:"开始放置"
  },

  onLoad: function () {
    var that = this;
    this.setVersion();
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          nickName: res.userInfo.nickName,
          userImg: res.userInfo.avatarUrl
        })
      }
    })
    this.userLogin();
    app.getMonstersAndConfigs();
    this.controlLoding();
    this.getSystermMsg();
  },
  setVersion:function(){
    var that = this;
    wx.request({
      url: app.appSetting.host + "weixin/GetVersion",
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
     
        that.setData({
          version: res.data.Result
        })

      }
    })
  },
  beginSet:function(){
      this.setData({
          btnname:"放置中..."
      });
      setInterval(function(){
        app.showMsg("+300猫晶");
      },10000)
  },
  //检测登录
  userLogin: function () {
    // 查看是否授权
    var that = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
             
              that.setData({
                nickName: res.userInfo.nickName,
                userImg: res.userInfo.avatarUrl
              })
              app.wxLogin(res.userInfo);
            }
          })
        } else {
          
          that.setData({
            isShowLogin: true
          })
        }
      }
    })
  },
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo == undefined) {
      //用户拒绝授权进行处理
      wx.showModal({
        title: '提示',
        content: '您点击了拒绝授权,将无法正常使用,点击确定重新获取授权。',
        success: function (res) {

        }
      })
    } else {
      //授权成功
      this.setData({
        isShowLogin: false
      })
      wx.getUserInfo({
        success: function (res) {
         
          that.setData({
            nickName: res.userInfo.nickName,
            userImg: res.userInfo.avatarUrl
          })
          
        }
      })
      app.wxLogin(e.detail.userInfo);
    }
  },
  wujinFight:function(){
    app.showMsg("开发中");
  },

  onShow: function () {
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  fightSet:function(){
    wx.navigateTo({
      url: 'setFight',
    })
  },
  goToResult:function(){
    wx.redirectTo({
      url: '/pages/fight/fightResult?power=300&price=1230&soure=900&hardtype=2',
    })
  },
  goToWanfa:function(){
    wx.navigateTo({
      url: '/pages/index/wanfa',
    })
  },
  controlLoding:function(){
    var that = this;
      var interval=setInterval(function(){
        if (commonData.powers.length>0) {
          that.setData({
            isShow:false
          });
          clearInterval(interval);
        }
      },1000);
  },
  //获取屏幕高度，宽度
  getSystermMsg: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        commonData.screenInfo.height = res.windowHeight;
        that.setData({
          height: res.windowHeight
        })
      }
    })
  },
  goToFight: function (e) {
    var hardtype = e.currentTarget.dataset.hardtype;
    wx.redirectTo({
      url: '/pages/fight/fight?hardtype='+hardtype,
    })
  },
  showAllTop:function(){
      wx.navigateTo({
        url: '/pages/user/UserRankings',
      })
  },
  sign:function(){
    var that = this;
    wx.request({
      url: app.appSetting.host + "weixin/SignDay?userId=" + app.globalData.userInfo.UserId,
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
      
        if (res.data.Status == 0) {
          app.showMsg("+3000猫晶");
          app.getCurrentUserPrice();
        } else {
          app.showMsg(res.data.Message);
        }

      }
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '快来参与保卫猫园守护战',
      path: '/pages/index/index',
      success: function (res) {
        //getSystemInfo是为了获取当前设备信息，判断是android还是ios，如果是android
        //还需要调用wx.getShareInfo()，只有当成功回调才是转发群，ios就只需判断shareTickets
        wx.getSystemInfo({
          success: function (d) {
           // console.log(d.platform);
            //console.log(res.shareTickets);
            if (res.shareTickets == null) {
              
              return;
            }
            if (d.platform == 'android' && (res.shareTickets.length > 0)) {
              wx.getShareInfo({
                shareTicket: res.shareTickets,
                success: function () {
                  //console.log("安卓群");
                  //app.sharePage();
                  wx.showToast({
                    title: "分享成功",
                    duration: 2000
                  });
                },
                fail: function () {
                  console(213123);
                  wx.showToast({
                    title: "分享成功",
                    duration: 2000
                  });
                }
              })
              return;
            }
            if (d.platform == 'ios' && (res.shareTickets.length > 0)) {

              //console.log("IOS群");
              //app.sharePage();
              wx.showToast({
                title: "分享成功",
                duration: 2000
              });
            } else {
              wx.showToast({
                title: "分享成功",
                duration: 2000
              });
            }
          },
          fail: function (res) {
            wx.showToast({
              title: "转发失败",
              duration: 2000
            });
          }
        })
      },
      fail: function (res) {
        // 转发失败
        console.log("取消转发");
      }
    }
  }

})
